﻿using System;
using System.Collections.Generic;
using System.Linq;

using System.Data;
using Prodma.DataAccess.Database;
using Prodma.DataAccess.Interfaces.Models;
using Prodma.DataAccess.Models.ViewModels;
using Prodma.DataAccess.Helper.Enumerators;
using System.Configuration;

namespace Prodma.DataAccess.DataService
{
    public class SepetService 
    {
        private DataContext context = new DataContext();

        public IList<SepetVm> GetList(int userId)
        {
            using (context)
            {
                var list = (from sepet in context.tb_Sepet
                            where sepet.tb_User.ID == userId &&
                            sepet.AktifPasif == true
                            select new SepetVm()
                            {
                                ID = sepet.ID,
                                UrunID = new UrunlerVm()
                                {
                                    ID = sepet.STOK.ID,
                                    UrunAdi = sepet.STOK.AD,
                                    Fiyat = sepet.STOK.FIYAT_SATIS.OrderByDescending(t => t.TARIH).Select(f => f.FIYAT).FirstOrDefault(),
                                    //Marka = new MarkaVm() 
                                    //{
                                    //    ID = sepet.tb_Urunler.tb_Marka.ID, 
                                    //    MarkaAdi = sepet.tb_Urunler.tb_Marka.MarkaAdi, 
                                    //    MarkaKodu = sepet.tb_Urunler.tb_Marka.MarkaKodu
                                    //}
                                },
                                Miktar = sepet.Miktar,
                                Aciklama = sepet.Aciklama
                            }).ToList();

                System.Web.HttpContext.Current.Session["chart"] = list;
                return list;
            }
        }
        public IList<SepetVm> GetOffer(int userId, decimal karMarji)
        {
            using (context)
            {
                var list = (from sepet in context.tb_Sepet
                            where sepet.tb_User.ID == userId &&
                            sepet.AktifPasif == true
                            select new SepetVm()
                            {
                                ID = sepet.ID,
                                UrunID = new UrunlerVm()
                                {
                                    ID = sepet.STOK.ID,
                                    UrunAdi = sepet.STOK.AD,
                                    Fiyat = sepet.STOK.FIYAT_SATIS.OrderByDescending(t => t.TARIH).Select(f => f.FIYAT * karMarji).FirstOrDefault(),
                                    //Marka = new MarkaVm() 
                                    //{
                                    //    ID = sepet.tb_Urunler.tb_Marka.ID, 
                                    //    MarkaAdi = sepet.tb_Urunler.tb_Marka.MarkaAdi, 
                                    //    MarkaKodu = sepet.tb_Urunler.tb_Marka.MarkaKodu
                                    //}
                                },
                                Miktar = sepet.Miktar,
                                Aciklama = sepet.Aciklama
                            }).ToList();

                System.Web.HttpContext.Current.Session["chart"] = list;
                return list;
            }
        }

        #region IRecordOperation<tb_Sepet,SepetVm> Members

        public IList<SepetVm> GetList()
        {
            throw new NotImplementedException();
        }

        public void New(tb_Sepet entity)
        {
            DataContext context = new DataContext();
            using (context)
            {
                context.AddTotb_Sepet(entity);
                context.SaveChanges();
            }
        }

        public SepetVm Edit(int id)
        {
            throw new NotImplementedException();
        }

        public void Update(int id, tb_Sepet entity)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var edit = (from sepet in context.tb_Sepet
                            where sepet.ID == id
                            select sepet).FirstOrDefault();

                edit.Miktar = entity.Miktar;

                context.SaveChanges();
            }
        }        

        public void Delete(int id)
        {
            if (id == 1)
            {
                return;
            }
            DataContext context = new DataContext();
            using (context)
            {
                var delete = (from sepet in context.tb_Sepet
                              where sepet.ID == id
                              select sepet).FirstOrDefault();

                context.DeleteObject(delete);
                context.SaveChanges();
            }
        }

        public void ClearChart(IList<SepetVm> sepetim)
        {
            foreach (var item in sepetim)
            {
                var urun = (from spt in context.tb_Sepet
                            where spt.ID == item.ID
                            select spt).First();

                context.DeleteObject(urun);
            }
            context.SaveChanges();
        }

        public void AddSiparisNot(int id, tb_Sepet entity)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var edit = (from sepet in context.tb_Sepet
                            where sepet.ID == id
                            select sepet).FirstOrDefault();

                edit.Aciklama = entity.Aciklama;

                context.SaveChanges();
            }
        }

        #endregion

        #region ISepetRepository Members


        public void AddChart(int userId,int productId, int quantity)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var edit = (from sepet in context.tb_Sepet
                            where sepet.tb_User.ID == userId && sepet.STOK.ID == productId
                            select sepet).FirstOrDefault();

                edit.Miktar = edit.Miktar + quantity;

                context.SaveChanges();
            }
        }

        #endregion

        #region IRowCountOperation Members

        public int GetTotalRowCount()
        {
            DataContext context = new DataContext();
            using (context)
            {
                int rowCount = (from sepet in context.tb_Sepet
                                select sepet).Count();

                return rowCount;
            }
        }
        public int GetTotalRowCount(Dictionary<string, string> find)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
